package com.pipayshop.controller;

import com.pipayshop.entity.vo.PageDataVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopCollectionService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoqiu
 * @date 2024/9/20 15:00
 * @description 网店商品收藏接口
 */
@RestController
@RequestMapping("/pipayshopapi/collection")
@Slf4j
public class OnlineStoreShopCollectionController {

    @Resource
    private OnlineStoreShopCollectionService shopCollectionService;

    /**
     * 根据用户id查询收藏列表
     * @return
     */
//    @GetMapping("/selectAllCollection/{userId}")
//    @ApiOperation("根据用户id查询收藏列表")
//    public ResponseVO selectAllCollection(@PathVariable String userId) {
//        return ResponseVO.getSuccessResponseVo(shopCollectionService.selectAllCollection(userId));
//    }

    //改进的（y）
    @GetMapping("/selectAllCollection/{page}/{limit}/{userId}")
    @ApiOperation("根据用户id查询收藏列表")
    public ResponseVO<PageDataVO> selectAllCollection(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable("userId") String userId) {
        try {
            PageDataVO collectList = shopCollectionService.selectAllCollection(page, limit, userId);
            return ResponseVO.getSuccessResponseVo(collectList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_failed");
        }
    }


    /**
     * 网店商品收藏新增
     * @return
     */
    @PostMapping("AddShopCommodityToCollection/{userId}/{commodityId}")
    @ApiOperation("网店商品收藏新增接口")
    public ResponseVO AddShopCommodityToCollection(@PathVariable String userId, @PathVariable String commodityId) {
        try {
            int insert = shopCollectionService.AddShopCommodityToCollection(userId, commodityId);
            if (insert < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("follow_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("follow_failed");
        }
    }


    /**
     *网店商品收藏删除
     */
    @PostMapping("closeShopCommodityToCollection/{userId}/{commodityId}")
    @ApiOperation("网店商品收藏删除接口")
    public ResponseVO<String> closeShopCommodityToCollection(@PathVariable String userId, @PathVariable String commodityId) {
        try {
            int update = shopCollectionService.closeShopCommodityToCollection(userId, commodityId);
            if (update < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("unfollow_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("unfollow_failed");
        }
    }

    /**
     * 校验该网店商品是否已经被收藏
     */
    @GetMapping("isShopCommodityToCollection/{userId}/{commodityId}")
    @ApiOperation("校验该网店商品是否已经被收藏接口")
    public ResponseVO<Boolean> isShopCommodityToCollection(@PathVariable String userId, @PathVariable String commodityId) {
        try {
            boolean flag = shopCollectionService.isShopCommodityToCollection(userId, commodityId);
            return ResponseVO.getSuccessResponseVo(flag);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("validate_product_failed");
        }
    }
}
